﻿using Jn698._45GWTool.Settings;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;

namespace Jn698._45GWTool
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static new App Current => (App)Application.Current;
        public Configuration Cfg { get; }
        //public static AppConfig AppConfig { get; set; } = new();
        public static string ConfigDict = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Configs");
        public static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        protected override void OnStartup(StartupEventArgs e)
        {
            //设置该软件的工作目录为当前软件目录
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            var xx = Directory.GetCurrentDirectory();
            //指定log4net的配置文件
            log4net.Config.XmlConfigurator.Configure(new FileInfo(Directory.GetCurrentDirectory() + "/Configs/Log4netConfig/log4net.config"));
            log.Info("App启动中...");
            //读取配置文件
            /*try
            {
                string configStr = Common.Utils.ReadJsonFile(Path.Combine(ConfigDict, "AppConfig.jsonAppConfig"));
                if (!string.IsNullOrWhiteSpace(configStr))
                {
                    AppConfig = JsonConvert.DeserializeObject<AppConfig>(configStr)!;
                }
            }
            catch (Exception ex)
            {
                log.Info("读取App配置文件失败", ex);
            }*/
            base.OnStartup(e);
        }
        public App()
        {
            Cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (Cfg.GetSection("commSettings")==null)
            {
                Cfg.Sections.Add("commSettings",new CommSettings());
                Cfg.Save();
            }
            if (Cfg.GetSection("encryptorSettings") == null)
            {
                Cfg.Sections.Add("encryptorSettings", new EncryptorSettings());
                Cfg.Save();
            }
            if (Cfg.GetSection("terminalSettings") == null)
            {
                Cfg.Sections.Add("terminalSettings", new TerminalSettings());
                Cfg.Save();
            }
            //var settings = (EncryptorSettings)Cfg.GetSection("encryptorSettings");

        }
    }
}
